/**
 * 总账
 */
$(function() {
	//根据条件查看总账
	$("#search").click(function(){
		var captionId = $("#ac").combobox('getValue');
		var year = $("#year").combobox('getValue');
		if(captionId==""||year==""){
			$.messager.alert("提示", "请输入完整的查询条件", "error");
			return;
		}else{
			$.ajax({
		        url:easyExt.url+'/generalLedger/get?captionId='+captionId+"&year="+year,
		        dataType:'json',
		        success:function(data){
		        	if(data.success==false){
		        		$.messager.alert("提示", data.msg, "error"); 
	    			}else{
	    				$('#dg').find("tbody").empty();
	    				for(var o in data){
	    					//判断期初余额是否为0，为0即显示“平”，否则不变
	    					if(data[o].openingBalance==0){
		    					balanceDirection="平";
		    				}else{
		    					balanceDirection=data[o].balanceDirection;
		    				}
	    					//添加期初余额行
		    				$('#dg').find("tbody").append("<tr><td rowspan='3'>"+data[o].date+"</td><td>期初余额</td><td></td><td></td><td>"+balanceDirection+"</td><td>"+data[o].openingBalance+"</td></tr>");
	    					//判断计算后的余额，显示相应的方向，余额取绝对值
		    				if(data[o].balanceDirection=="借"){
	    						var a=easyExt.accAdd(Number(data[o].openingBalance),Number(data[o].jffs));
	    						balance=easyExt.accSub(a,Number(data[o].dffs));
	    						if(Number(balance)==0){
			    					balanceDirection="平";
			    				}
		    					else if(Number(balance)>0){
			    					balanceDirection="借";
			    				}else if(Number(balance)<0){
			    					balance=Math.abs(Number(balance));
			    					balanceDirection="贷";
			    				}
	    					}else{
	    						var a=easyExt.accAdd(Number(data[o].openingBalance),Number(data[o].dffs));
	    						balance=easyExt.accSub(a,Number(data[o].jffs));
//	    						balance=Number(data[o].openingBalance)-Number(data[o].jffs)+Number(data[o].dffs);
	    						if(Number(balance)==0){
			    					balanceDirection="平";
			    				}
		    					else if(Number(balance)>0){
			    					balanceDirection="贷";
			    				}else if(Number(balance)<0){
			    					balance=Math.abs(Number(balance));
			    					balanceDirection="借";
			    				}
	    					}
	    					$('#dg').find("tbody").append("<tr><td>本月合计</td><td>"+data[o].jffs+"</td><td>"+data[o].dffs+"</td><td>"+balanceDirection+"</td><td>"+balance+"</td></tr>" +
	    							"<tr><td>本年累计</td><td>"+data[o].ljjf+"</td><td>"+data[o].ljdf+"</td><td>"+balanceDirection+"</td><td>"+balance+"</td></tr>");
			        		
			        	}
	    			}
		        },
				error : function() {
					$.messager.alert('提示', "错误", "error");
				}
		     });
		}
	});
});

function showSL(date){
	var captionId = $("#ac").combobox('getValue');
	alert(captionId+"-----"+date);
}